﻿using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace EFCoreDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string strConn = "Server=127.0.0.1;database=wimanu_center;uid=root;pwd=wisekoo2021;charset=utf8;SslMode=None";
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDBContext>();
            optionsBuilder.UseMySql(strConn, ServerVersion.AutoDetect(strConn));

            using (var DB = new ApplicationDBContext(optionsBuilder.Options))
            {
                var users = DB.Users.AsNoTracking().OrderBy(user => user.Id).Take(10).ToListAsync().Result;
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.Id} {user.Name}");
                }
            }
            Console.ReadKey();
        }
    }

    public class ApplicationDBContext : DbContext
    {
        public DbSet<User> Users { get; set; }

        public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
        {
        }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseMySql("Server=127.0.0.1;database=wimanu_center;uid=root;pwd=;charset=utf8;SslMode=None");
        //}
    }

    [Table("users")]
    public class User
    {
        [Column("id")]
        public int Id { get; set; }
        [Column("name")]
        public string Name { get; set; }
    }
}
